package com.example.lt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.lt.constant.MessageConstant;
import com.example.lt.pojo.entity.ListSong;
import com.example.lt.pojo.vo.SongVO;
import com.example.lt.result.Result;
import com.example.lt.service.ListSongService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 小涛Tao
 * @Date: 2024/06/02/19:07
 * version 1.0 注释：
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/listSong")
public class ListSongController {

    private final ListSongService listSongService;

    /**
     * 管理端给歌单添加歌曲
     * @return
     */
    @PostMapping("/admin/addSong")
    @CacheEvict(value = "songList", key = "#listSong.songListId")
    public Result addSong(@RequestBody ListSong listSong) {
        log.info("管理端给歌单添加歌曲：{}",listSong);
        boolean flag = listSongService.save(listSong);
        if (flag) {
            return Result.success(MessageConstant.LIST_SONG_ADD_SUCCESS);
        } else {
            return Result.error(MessageConstant.LIST_SONG_ADD_FAIL);
        }
    }

    /**
     * 管理端删除歌单中的歌曲
     * @return
     */
    @PostMapping("/admin/deleteSong")
    @CacheEvict(value = "songList", key = "#listSong.songListId")
    public Result deleteSong(@RequestBody ListSong listSong) {
        log.info("管理端删除歌单中的歌曲：{}",listSong);
        LambdaQueryWrapper<ListSong> lambda = new QueryWrapper<ListSong>()
                .eq("song_id", listSong.getSongId())
                .eq("song_list_id", listSong.getSongListId())
                .lambda();

        boolean flag = listSongService.remove(lambda);
        if (flag) {
            return Result.success(MessageConstant.LIST_SONG_DELETE_SUCCESS);
        } else {
            return Result.error(MessageConstant.LIST_SONG_DELETE_FAIL);
        }
    }

    /**
     * 客户端、管理端 根据歌单id查询歌单中的所有的歌曲
     * @param songListId
     * @return
     */
    @GetMapping("/common/bySongList")
    public Result<List<SongVO>> getSongByListId(@RequestParam int songListId) {
        log.info("根据歌单id查询歌单中的所有的歌曲：{}", songListId);
        List<SongVO> songVOList = listSongService.getSongList(songListId);
        return Result.success(MessageConstant.SEEK_SUCCESS,songVOList);
    }

}
